package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsNarratorDetail;
import com.macro.mall.dto.BmsNarratorParam;
import com.macro.mall.mapper.BmsNarratorMapper;
import com.macro.mall.model.BmsNarrator;
import com.macro.mall.model.BmsNarratorExample;
import com.macro.mall.portal.service.BmsNarratorPicService;
import com.macro.mall.portal.service.BmsNarratorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class BmsNarratorServiceImpl implements BmsNarratorService {

    @Autowired
    private BmsNarratorMapper bmsNarratorMapper;

    @Autowired
    private BmsNarratorPicService bmsNarratorPicService;


    @Override
    public List<BmsNarrator> getList(BmsNarratorParam bmsNarratorParam) {
        Integer pageNum = bmsNarratorParam.getPageNum();
        Integer pageSize = bmsNarratorParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
        BmsNarratorExample example = new BmsNarratorExample();
        BmsNarratorExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        example.setOrderByClause("create_time desc");
        List<BmsNarrator> list = bmsNarratorMapper.selectByExample(example);
        return list;
    }

    @Override
    public BmsNarratorDetail getItem(Long id) {
        BmsNarrator bmsNarrator = bmsNarratorMapper.selectByPrimaryKey(id);
        BmsNarratorDetail detail = new BmsNarratorDetail();
        BeanUtils.copyProperties(bmsNarrator, detail);
        // 获取封面图
        String coverPic = bmsNarratorPicService.findCoverPicByActivitiesId(bmsNarrator.getId());
        // 获取活动图片
        List<String> picList = bmsNarratorPicService.findPicListByActivitiesId(bmsNarrator.getId());

        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }

    @Override
    public BmsNarratorDetail getDefaultProject() {
        BmsNarratorExample example = new BmsNarratorExample();
        BmsNarratorExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        example.setOrderByClause("create_time desc");
        List<BmsNarrator> list = bmsNarratorMapper.selectByExample(example);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        BmsNarratorDetail detail = new BmsNarratorDetail();
        BmsNarrator bmsNarrator = list.get(0);
        BeanUtils.copyProperties(bmsNarrator, detail);
        // 获取封面图
        String coverPic = bmsNarratorPicService.findCoverPicByActivitiesId(bmsNarrator.getId());
        // 获取活动图片
        List<String> picList = bmsNarratorPicService.findPicListByActivitiesId(bmsNarrator.getId());

        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }

    @Override
    public BmsNarratorDetail getProjectByMuseumId(Long museumId) {
        BmsNarratorExample example = new BmsNarratorExample();
        BmsNarratorExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo((byte) 0);
        criteria.andMuseumIdEqualTo(museumId);
        example.setOrderByClause("create_time desc");
        List<BmsNarrator> list = bmsNarratorMapper.selectByExample(example);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        BmsNarratorDetail detail = new BmsNarratorDetail();
        BmsNarrator bmsNarrator = list.get(0);
        BeanUtils.copyProperties(bmsNarrator, detail);
        // 获取封面图
        String coverPic = bmsNarratorPicService.findCoverPicByActivitiesId(bmsNarrator.getId());
        // 获取活动图片
        List<String> picList = bmsNarratorPicService.findPicListByActivitiesId(bmsNarrator.getId());

        detail.setCoverPic(coverPic);
        detail.setPicList(picList);
        return detail;
    }

}
